@ ECHO OFF

rem ** Program:
rem **        Log the memory information of the specified process periodically.
rem ** History:
rem **       2012/08/23  Version: 0.0.3
rem ** Change:
rem **       Add additional options to specifiy interesting process and log period.

rem SETLOCAL ENABLEDELAYEDEXPANSION
SET myDevice=%1
SET myProcess=%2
SET myPeriod=%3

if (%myDevice%)==() (
    ECHO "Usage: %0 DEVICE [PROCESS] [PERIOD]"
    GOTO :EOF
) 

if (%myProcess%)==() (
    SET myProcess=system
)

if (%myPeriod%)==() (
    SET myPeriod=300
)


CALL :PRINT_SYSTEM_TIME
CALL :PRINT_SYSTEM_TIME > %myProcess%_%myDevice%.meminfo
CALL :PRINT_DEVICE_TIME
CALL :PRINT_DEVICE_TIME >> %myProcess%_%myDevice%.meminfo
adb -s %myDevice% shell dumpsys meminfo %myProcess%
adb -s %myDevice% shell dumpsys meminfo %myProcess% >> %myProcess%_%myDevice%.meminfo
ping 127.0.0.1 -n %myPeriod% -w 1000 > nul

FOR /L %%N IN (1 0 10) do (
    CALL :PRINT_SYSTEM_TIME
    CALL :PRINT_SYSTEM_TIME >> %myProcess%_%myDevice%.meminfo
    CALL :PRINT_DEVICE_TIME
    CALL :PRINT_DEVICE_TIME >> %myProcess%_%myDevice%.meminfo
    adb -s %myDevice% shell dumpsys meminfo %myProcess% 
    adb -s %myDevice% shell dumpsys meminfo %myProcess% >> %myProcess%_%myDevice%.meminfo
    ping 127.0.0.1 -n %myPeriod% -w 1000 > nul
)


:PRINT_SYSTEM_TIME
FOR /F "tokens=1-4 delims=-/ " %%i IN ("%date%") DO (
    SET year=%%i
    SET month=%%j
    SET day=%%k
)

FOR /f "tokens=1-4 delims=:." %%i IN ("%time%") DO (
    SET hour=%%i
    SET minute=%%j
    SET second=%%k
    SET centisecond=%%l
)
ECHO [SYSTEM] %month%-%day% %hour%:%minute%:%second%
goto :EOF

:PRINT_DEVICE_TIME
 adb -s %myDevice% shell "date +'[DEVICE] %%m-%%d %%H:%%M:%%S'"
goto :EOF
